package com.example.sharesinfo.fragment.index;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.example.sharesinfo.R;
import com.example.sharesinfo.adapter.MyPagerAdapter;
import com.example.sharesinfo.base.BaseFragment;
import com.example.sharesinfo.fragment.StockFragment;
import com.example.sharesinfo.ui.AIActivity;
import com.example.sharesinfo.ui.LongHuBangActivity;
import com.example.sharesinfo.ui.QAActivity;
import com.example.sharesinfo.ui.TingFuPaiActivity;
import com.example.sharesinfo.ui.WordSearchActivity;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;


public class IStockFragment extends BaseFragment implements View.OnClickListener {
    private IStockFragment fa;
    private List<Fragment> fragments = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private TabLayout mTableLayout;
    private ViewPager mViewPager;
    private RecommendFragment mRecommendFragment = new RecommendFragment();
    private NewsFlashFragment mNewsFlashFragment = new NewsFlashFragment();
    private QuotationFragment mQuotationFragment = new QuotationFragment();
    private FocusOnFragment mFocusOnFragment = new FocusOnFragment();
    //龙虎榜
    private LinearLayout ll_lhb;
    //问答
    private LinearLayout ll_qa;
    private AppCompatActivity activity;
    //自选股
    private LinearLayout ll_option;
    //社区
    private LinearLayout ll_community;
    //沪深
    private ImageView iv_husheng;
    private TextView tv_husheng;
    //港股
    private ImageView iv_global;
    private TextView tv_global;
    //盘面指数
    private ImageView iv_dp;
    private TextView tv_dp;
    //股市词典
    private ImageView iv_cd;
    private TextView tv_cd;
    //停复牌
    private ImageView iv_tfp;
    private TextView tv_tfp;
    //问财
    private ImageView iv_wc;
    private TextView tv_wc;

    public IStockFragment getFragment() {
        if (fa == null) {
            fa = new IStockFragment();
        }
        return fa;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_istock, null);
        activity = (AppCompatActivity) getActivity();
        initData();
        initView(view);
        return view;
    }

    private void initData() {
        fragments.add(mRecommendFragment);
        fragments.add(mFocusOnFragment);
        fragments.add(mNewsFlashFragment);
        fragments.add(mQuotationFragment);
        titles.add("推荐");
        titles.add("关注");
        titles.add("快讯");
        titles.add("盘面");

    }

    private void initView(View view) {
        ll_lhb = view.findViewById(R.id.ll_lhb);
        ll_lhb.setOnClickListener(this);
        ll_qa = view.findViewById(R.id.ll_qa);
        ll_qa.setOnClickListener(this);
        ll_option = view.findViewById(R.id.ll_option);
        ll_option.setOnClickListener(this);
        ll_community = view.findViewById(R.id.ll_community);
        ll_community.setOnClickListener(this);
        iv_husheng = view.findViewById(R.id.iv_husheng);
        tv_husheng = view.findViewById(R.id.tv_husheng);
        iv_husheng.setOnClickListener(this);
        tv_husheng.setOnClickListener(this);
        iv_global = view.findViewById(R.id.iv_global);
        tv_global = view.findViewById(R.id.tv_global);
        iv_global.setOnClickListener(this);
        tv_global.setOnClickListener(this);
        iv_dp = view.findViewById(R.id.iv_dp);
        tv_dp = view.findViewById(R.id.tv_dp);
        iv_dp.setOnClickListener(this);
        tv_dp.setOnClickListener(this);
        iv_cd = view.findViewById(R.id.iv_cd);
        tv_cd = view.findViewById(R.id.tv_cd);
        iv_cd.setOnClickListener(this);
        tv_cd.setOnClickListener(this);
        iv_tfp = view.findViewById(R.id.iv_tfp);
        tv_tfp = view.findViewById(R.id.tv_tfp);
        iv_tfp.setOnClickListener(this);
        tv_tfp.setOnClickListener(this);
        iv_wc = view.findViewById(R.id.iv_wc);
        tv_wc = view.findViewById(R.id.tv_wc);
        iv_wc.setOnClickListener(this);
        tv_wc.setOnClickListener(this);
        mTableLayout = view.findViewById(R.id.tl_tabs);
        mTableLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        mViewPager = view.findViewById(R.id.vp_view);
        mViewPager.setAdapter(new MyPagerAdapter(getFragmentManager(), getContext(), fragments, titles));
        mTableLayout.setupWithViewPager(mViewPager);//此方法就是让tablayout和ViewPager联动
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_lhb:
                startActivity(new Intent(getContext(), LongHuBangActivity.class));
                break;
            case R.id.ll_qa:
                startActivity(new Intent(getContext(), QAActivity.class));
                break;
            case R.id.iv_cd:
            case R.id.tv_cd:
                startActivity(new Intent(getContext(), WordSearchActivity.class));
                break;
            case R.id.iv_tfp:
            case R.id.tv_tfp:
                startActivity(new Intent(getContext(), TingFuPaiActivity.class));
                break;
            case R.id.iv_wc:
            case R.id.tv_wc:
                startActivity(new Intent(getContext(), AIActivity.class));
                break;
            case R.id.ll_option:
                RadioButton main_optional = activity.findViewById(R.id.radio_button_optional);
                main_optional.setChecked(true);
                break;
            case R.id.ll_community:
                RadioButton main_community = activity.findViewById(R.id.radio_button_community);
                main_community.setChecked(true);
                break;
            case R.id.iv_dp:
            case R.id.tv_dp:
                RadioButton main_market2 = activity.findViewById(R.id.radio_button_market);
                main_market2.setChecked(true);
                break;
            case R.id.iv_husheng:
            case R.id.tv_husheng:
                RadioButton main_market = activity.findViewById(R.id.radio_button_market);
                main_market.setTag("husheng");
                main_market.setChecked(true);
                break;
            case R.id.iv_global:
            case R.id.tv_global:
                RadioButton main_market1 = activity.findViewById(R.id.radio_button_market);
                main_market1.setTag("global");
                main_market1.setChecked(true);
                break;
        }

    }
}
